package com.ht.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.ht.util.CommMethod;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @ClassName: DateConfig
 * @Author: hejialun
 * @Description: 统一时间格式处理
 * @Date: 2021/4/22 15:18
 */
@Configuration
public class DateConfig {
    /**
     * @Author hejialun
     * @Description: TODO(设置时间类型接口统一返回格式)
     * @Return
     * @Date 2021/5/1 14:56
     */
    @Bean(name = "mapperObject")
    public ObjectMapper getObjectMapper() {
        ObjectMapper om = new ObjectMapper();
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        om.registerModule(javaTimeModule);
        return om;
    }

    /**
     * @Author hejialun
     * @Description: TODO(日期转换器, java.lang.String -> java.util.Date)
     * @param
     * @Return org.springframework.core.convert.converter.Converter<java.lang.String,java.util.Date>
     * @Date 2021/4/22 15:19
     */
    @Bean
    public Converter<String, Date> dateConverter() {
        return new Converter<String, Date>() {

            @Override
            public Date convert(String source) {
                return CommMethod.parseDate(source);
            }
        };
    }



    /**
     * @Author hejialun
     * @Description: TODO(日期转换器, java.lang.String -> java.time.LocalDateTime)
     * @param
     * @Return org.springframework.core.convert.converter.Converter<java.lang.String,java.time.LocalDateTime>
     * @Date 2021/4/22 15:20
     */
    @Bean
    public Converter<String, LocalDateTime> localDateTimeConverter() {
        return new Converter<String, LocalDateTime>() {

            @Override
            public LocalDateTime convert(String source) {
                return CommMethod.parseLocalDateTime(source);
            }
        };
    }

    /**
     * @Author hejialun
     * @Description: TODO(日期转换器, java.lang.String -> java.time.LocalDate)
     * @param
     * @Return org.springframework.core.convert.converter.Converter<java.lang.String,java.time.LocalDate>
     * @Date 2021/4/22 15:20
     */
    @Bean
    public Converter<String, LocalDate> LocalDateConverter(){
        return new Converter<String, LocalDate>() {
            @Override
            public LocalDate convert(String source) {
                return CommMethod.parseLocalDate(source);
            }
        };
    }

}